<?php 
namespace App\Http\Controllers\MobileApi;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Mysql\pre_forum_post;
use App\Models\Mysql\pre_forum_post_tableid;
use App\Models\Mysql\pre_forum_thread;
//use App\Models\Mysql\pre_common_member_profile;

class PublishController extends Controller
{
	use GlobalData;

	const ROUTE_PREFIX = '/api/*';

	protected $request;

	protected $curTime;

	protected $debug;

	//const BILINGUAL_READING_CH = '经典双语阅读';

	//const PAGINATE_SIZE = 10; //手机分页每页显示记录的个数.

	/**
	 * 创建新的`PublishController`控制器实例
	 * @param \Illuminate\Http\Request $request
	 */
	public function __construct(Request $request)
	{
		$this->request = $request;
		$this->error = [];
		$this->curTime = time();

		if(config('app.debug')) {
			$this->debug = true;
			error_reporting(E_ALL | E_STRICT);
		} else {
			$this->debug = false;
		}
	}

	/**
	 * API: 发表回复接口(publishReply)
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function PublishReply()
	{
		try {
			//user related
			$curUser = getCurUser();
			if (empty($curUser)){
				return(json_encode(array('code' => 3,'msg' => '亲，请登录','result' => [])));
			}
			$curUid = $curUser['uid'];
			if (isCurUserCertified()==false){
				return(json_encode(array('code' => 4,'msg' => '亲，请完成实名认证','result' => [])));
			}

			//参数 related
			if( !$this->check($this->request)) return responseJson($this->getError());
			$p = $this->request;
			if( !isset($p['tid']) ){
				return(json_encode(array('code' => 5,'msg' => '未获取到参数: 主题id(tid)','result' => [])));
			} 
			if( !isset($p['content']) ) {
				return(json_encode(array('code' => 5,'msg' => '未获取到参数: 内容(content)','result' => [])));
			} 
			
			//insert pre_forum_post
			$param=array();
			$param['authorid'] = $curUid;
			$param['author'] = $curUser['user_name'];
			$param['tid'] = intval($p['tid']);
			$param['content'] = $p['content'];
			$param['dateline'] = time();

			$r=pre_forum_thread::getThreadInfo($param['tid']);
			if( empty($r) ){
				return(json_encode(array('code' => 6,'msg' => 'tid为'.$param['tid'].'的主题不存在','result' => [])));
			} 
			$param['fid']=$r[0]->fid;
			//echo "getThreadInfo success tid=".$param['tid']."\tcontent=".$param['content']."\tfid=".$param['fid']."</br>";
			
			$r=pre_forum_post_tableid::insert();
			if ($r[0]==0){
				return(json_encode(array('code' => 100,'msg' => '插入pre_forum_post_tableid表失败','result' => [])));
			}
			$param['pid']=$r[1];
			//echo "insert pre_forum_post_tableid success tid=".$param['tid']."\tcontent=".$param['content']."\tfid=".$param['fid']."\tpid=".$param['pid']."</br>";
			
			$r=pre_forum_post::insertReply($param);//var_dump($r);exit;
			if( empty($r) ){
				return(json_encode(array('code' => 100,'msg' => '插入pre_forum_post表失败','result' => [])));
			} 
			//echo "insert pre_forum_post success </br>";
			
//			if($param['pid'] % 1024 == 0) {
			if($param['pid'] >200 ) {
				$r=pre_forum_post_tableid::delete_by_lesspid($param['pid']);//$r is an int, the number of records deleted;
			}
			
			//update thread
			$updateThreadParam=array();
			$maxPosition = pre_forum_post::getMaxPositionByTid($param['tid']);
			$updateThreadParam ['maxposition'] = $maxPosition;
			
			$incThreadParam=array();
			$incThreadParam []= "replies";
			$r = pre_forum_thread::updateAndInc($param['tid'],$updateThreadParam, $incThreadParam); //$r is an int, the number of records updated;
			if( empty($r) ){
				return(json_encode(array('code' => 100,'msg' => '更新pre_forum_thread表失败','result' => [])));
			} 
			
			return json_encode(array('code' => 200,'msg' => '发表回复成功',
				'result' => array('pid'=>$param['pid'], 'authorName' => $curUser['user_name'], 'authorAvatarUrl' => avatarUrl($curUid),'publishTime'=>$param['dateline'])
				));
			//return responseJson('success', array('pid'=>$param['pid']), TRUE);

		} catch (\Exception $e) {
			if($this->debug) {
				return responseJson(__FILE__.':'.__FUNCTION__.':'.__LINE__.':'.$e->getMessage());
			}
			return responseJson($e->getMessage());
		}
	}

} //!- publishReplyController
?>
